// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: pengtianrui 2276608413@qq.com
 * @LastEditTime: 2024-12-23 13:44:10
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Load\DAREAParser.cpp
 */

#include "Import/BDFImport/BulkData/Load/DAREAParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/IDAREAService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (DAREA);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

// 以下需要修改
enum DAREAkey : std::uint8_t
{
    DAREAKEY_SID = 2,
    DAREAKEY_P1  = 3,
    DAREAKEY_C1  = 4,
    DAREAKEY_A1  = 5,
    DAREAKEY_P2  = 6,
    DAREAKEY_C2  = 7,
    DAREAKEY_A2  = 8,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseDAREA (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存DAREA数据的临时结构体对象
    SG::DataStructure::FEM::DAREAData tempDAREA1;
    SG::DataStructure::FEM::DAREAData tempDAREA2;
    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempDAREA1.m_domainId = tempDAREA2.m_domainId = 1;

    // 将输入的条目信息分割成单个词条

    // 初始化当前处理的字符串条目的ID
    int  curIDKey     = 1;
    bool isHaveSecond = false;

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 判断当前条目是否为空（即是否全为空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            // 如果当前条目为空，跳过当前循环并处理下一个条目
            curIDKey++;
            continue;
        }

                // 根据条目ID，将解析后的数值存入对应的DAREA字段
        switch (curIDKey)
        {
        case DAREAKEY_SID:
            // 解析SID并赋值
            tempDAREA1.m_sId = convertToI64 (curStrKey);
            break;
        case DAREAKEY_P1:
            tempDAREA1.m_P = convertToI64 (curStrKey);
            break;
        case DAREAKEY_C1:
            tempDAREA1.m_C = convertToI64 (curStrKey);
            break;
        case DAREAKEY_A1:
            tempDAREA1.m_A = convertToReal (curStrKey);
            break;
        case DAREAKEY_P2:
            isHaveSecond   = true;
            tempDAREA2.m_P = convertToI64 (curStrKey);
            break;
        case DAREAKEY_C2:
            tempDAREA2.m_C = convertToI64 (curStrKey);
            break;
        case DAREAKEY_A2:
            tempDAREA2.m_A = convertToReal (curStrKey);
            break;
        default:
            // 如果条目ID不匹配，跳过处理
            break;
        }

        // 增加条目ID，以便处理下一个条目
        curIDKey++;
    }

    if (isHaveSecond)
    {
        tempDAREA2.m_sId = tempDAREA1.m_sId;
        // 如果存在第二个DAREA数据，则将第二个DAREA数据添加到数据库中
        dbServiceFactory->get<IDAREAService> ()->Add (tempDAREA2);
    }
    // 将解析后的DAREA数据通过数据库服务工厂的DAREA服务添加到数据库中
    return dbServiceFactory->get<IDAREAService> ()->Add (tempDAREA1);
}